package com.tianyang.yukuaifuzs.business.auth.presenter;

import com.alibaba.fastjson.JSONObject;
import com.tianyang.yukuaifuzs.base.BaseTitleActivity;
import com.tianyang.yukuaifuzs.business.auth.contract.MerchantRefuseContract;
import com.tianyang.yukuaifuzs.business.auth.model.AuthModel;
import com.tianyang.yukuaifuzs.business.main.bean.PosUserOutput;
import com.tianyang.yukuaifuzs.common.interfaces.CommonConfig;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：商户已拒绝
 * Created by xyj on 2024/3/8
 */
public class MerchantRefusePresenter implements MerchantRefuseContract.Presenter {

    private MerchantRefuseContract.View mView;

    private CompositeSubscription mSubscription;

    public MerchantRefusePresenter(MerchantRefuseContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void getMerchantRefuseList(int pageNumber, String searchKey) {
        Subscription subscription = AuthModel.getInstance().posUserManage(pageNumber, searchKey, "-1")
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<PosUserOutput>>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showMerchantRefuseListView(null, pageNumber == CommonConfig.PAGE_NUM);
                        mView.showLoadMore(CommonConfig.LOADMOREFAIL);
                    }

                    @Override
                    public void onNext(List<PosUserOutput> outputs) {
                        mView.showMerchantRefuseListView(outputs, pageNumber == CommonConfig.PAGE_NUM);
                        if (outputs != null) {
                            if (outputs.size() < CommonConfig.PAGE_SIZE) {
                                mView.showLoadMore(CommonConfig.LOADMOREEND);
                            } else {
                                mView.showLoadMore(CommonConfig.LOADMORECOMPLETE);
                            }
                        } else {
                            mView.showLoadMore(CommonConfig.LOADMOREEND);
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void saveIdCertTemp(String tel, String merchantType) {
        ((BaseTitleActivity) mView.getContext()).showLoadingDialog(false);
        Subscription subscription = AuthModel.getInstance().saveIdCertTemp(tel, merchantType)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<JSONObject>() {
                    @Override
                    public void onCompleted() {
                        ((BaseTitleActivity) mView.getContext()).dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        ((BaseTitleActivity) mView.getContext()).dismissLoadingDialog();
                        ((BaseTitleActivity) mView.getContext()).showErrorText(e);
                    }

                    @Override
                    public void onNext(JSONObject obj) {
                        mView.showSaveSuccess();
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
